var config = require('../config');
var db = config.db;

exports.index = function(req,res,next){
	db.query('select * from todo order by finished asc,id asc limit 50',function(err,rows){
		if(err) return next(err);
		res.render('index',{todos:rows});
	});

	
}

exports.new = function(req, res, next) {
    var title = req.body.title || '';
    title = title.trim();
	console.log(title);
    if(!title) {
        return res.render('error', {message: '标题是必须的'});
    }
    db.query('insert into todo set title=?, post_date=now()', [title], function(err, result) {
        if(err) return next(err);
        res.redirect('/');
    });
	
};

exports.delete = function(req,res,next){
	var id = req.params.id;
	console.log(id);
	
	db.query('delete from todo where id = ?',[id],function(err){
		if(err) return next(err);
		res.redirect('/');
	});
	
};






